using DA.AnimGraph;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace DA.AnimGraph.Editor {

    /// <summary>
    /// 收集 <see cref="TimelineEventParameter"/> 的子类，添加到timeline菜单
    /// </summary>
    public class TimelineEventTypeRegister {
        private static TimelineEventTypeRegister instance;

        Dictionary<string, Type> types;

        public static TimelineEventTypeRegister GetInstance() {
            if (instance == null) {
                instance = new();
            }
            return instance;
        }

        private TimelineEventTypeRegister() {
            InitEvents();
        }

        private void InitEvents() {
            types = new Dictionary<string, Type>();
            Type baseType = typeof(TimelineEventParameter);
            var subClassList = TypeRegisterUtility.GetAllSubClasses(baseType);
            foreach (var subClass in subClassList) {
                types.Add(subClass.Name, subClass);
            }
        }

        public Dictionary<string, Type> GetTimelineEventTypes() {
            if (types == null || types.Count == 0) {
                InitEvents();
            }
            if (types.Count == 0) {
                //throw new Exception("cant find any Timeline Event");
                D.W("未定义任何 Timeline Event。这是正常的，但是会导致无法使用Timeline。添加 Timeline Event 参考：TimelineEvent.cs");
                return null;
            }
            return types;
        }
    }

}

